package com.loveyukari.controller;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loveyukari.common.exception.InternalException;
import com.loveyukari.service.RandomService;
import com.loveyukari.support.JsonResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年2月16日下午1:08:35
**/
@Controller
@RequestMapping("/admin/random")
public class RandomController {

	@Autowired
	private RandomService randomService;
	
	private Logger log = LoggerFactory.getLogger(RandomController.class);
	
	@RequestMapping(value="/number",method=RequestMethod.GET)
	public String number(){
		return "/random/number";
	}
	@RequestMapping(value="/number",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult<List<Integer>> number(String max,String size,int status){
		try {
			int i = Integer.parseInt(max);
			int s = Integer.parseInt(size);
			List<Integer> c = randomService.randomInt(Math.abs(i),Math.abs(s),status);
			return new JsonResult<List<Integer>>(0,"",c);
		} catch (NumberFormatException e) {
			return new JsonResult<List<Integer>>(-1, "请不要乱输数字");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return new JsonResult<List<Integer>>(-1, "服务异常,请呼叫小马");
		}
	}
	
	@RequestMapping(value="/yuba",method=RequestMethod.GET)
	public String yuba(){
		return "/random/yuba";
	}
	@RequestMapping(value="/yuba",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult<Set<JSONObject>> yuba(String size,String url,int status,String comment){
		try {
			Set<JSONObject> set = randomService.randomYubaNumber(url, size, status);
			log.info(comment+";"+set);
			return new JsonResult<Set<JSONObject>>(0, "", set);
		} catch (InternalException e) {
			return new JsonResult<Set<JSONObject>>(-1, e.getMessage());
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return new JsonResult<Set<JSONObject>>(-1, "服务异常，请呼叫小马");
		}
	}
	@RequestMapping(value="/bilibili",method=RequestMethod.GET)
	public String bilibili(){
		return "/random/bilibili";
	}
	@RequestMapping(value="/bilibili",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult<Object> bilibili(int size,int av,boolean repeat){
		JSONObject data;
		if (repeat){
			data = randomService.randomBilibiliCommentRepeat(av, size);
		} else {
			data = randomService.randomBilibiliComment(av, size);
		}
		return new JsonResult<Object>(0,"",data);
	}
	
	@RequestMapping("/bilibili/show")
	public String bilibiliShow(Model model){
		JSONArray authArray = randomService.getAuthArray();
		model.addAttribute("authArray", authArray);
		return "/random/bilibili_show";
	}
	
	
}
